﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BTN04.DTO;
using BTN04.BUS;

namespace BTN04.USER
{
    public partial class lichsumuahang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDS();
        }

        public void LoadDS()
        {
            NguoiDungBUS ndBUS = new NguoiDungBUS();
            NguoiDungDTO ndDTO = new NguoiDungDTO();
            HoaDonBUS hdBUS = new HoaDonBUS();
            LoaiSanPhamBUS lspBUS = new LoaiSanPhamBUS();
            NhaSanXuatBUS nsxBUS = new NhaSanXuatBUS();

            string tendangnhap = Session["tendn"].ToString();
            ndDTO = ndBUS.LayThongTinNguoiDungTheoTenDangNhap(tendangnhap);
            int mand = int.Parse(ndDTO.MaNguoiDung.ToString());

            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            dt = hdBUS.SanPhamCuaKhachHangDaMua(mand);
            DataColumn c1 = dt1.Columns.Add("STT", Type.GetType("System.Int32"));
            DataColumn c2 = dt1.Columns.Add("TenSanPham", Type.GetType("System.String"));
            DataColumn c3 = dt1.Columns.Add("TenLoaiSanPham", Type.GetType("System.String"));
            DataColumn c4 = dt1.Columns.Add("TenNSX", Type.GetType("System.String"));
            DataColumn c5 = dt1.Columns.Add("GiaBan", Type.GetType("System.Double"));
            DataColumn c6 = dt1.Columns.Add("SoLuong", Type.GetType("System.Int32"));
            DataColumn c7 = dt1.Columns.Add("ThoiGianMua", Type.GetType("System.DateTime"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int masp = Convert.ToInt32(dt.Rows[i]["MaSanPham"].ToString());
                SanPhamDTO spDTO = new SanPhamDTO();
                spDTO = SanPhamBUS.LaySanPhamTheoMa(masp);
                LoaiSanPhamDTO lspDTO = new LoaiSanPhamDTO();
                lspDTO = lspBUS.LayLoaiSanPhamTheoMa(spDTO.MaLoaiSanPham);
                NhaSanXuatDTO nsxDTO = new NhaSanXuatDTO();
                nsxDTO = nsxBUS.LayNhaSanXuatTheoMa(spDTO.MaNhaSanXuat);
                DataRow r = dt1.NewRow();
                r[0] = (i + 1).ToString();
                r[1] = spDTO.TenSanPham.ToString();
                r[2] = lspDTO.TenLoaiSanPham.ToString();
                r[3] = nsxDTO.TenNhaSanXuat.ToString();
                int slmua = Convert.ToInt32(dt.Rows[i]["SoLuongBan"].ToString());
                r[4] = spDTO.GiaBan.ToString();
                r[5] = slmua.ToString();
                r[6] = dt.Rows[i]["ThoiGianMua"];
                dt1.Rows.Add(r);
            }
            gvSanPham.DataSource = dt1;
            gvSanPham.DataBind();
        }

        protected void gvSanPham_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            NguoiDungBUS ndBUS = new NguoiDungBUS();
            NguoiDungDTO ndDTO = new NguoiDungDTO();
            HoaDonBUS hdBUS = new HoaDonBUS();
            LoaiSanPhamBUS lspBUS = new LoaiSanPhamBUS();
            NhaSanXuatBUS nsxBUS = new NhaSanXuatBUS();

            string tendangnhap = Session["tendn"].ToString();
            ndDTO = ndBUS.LayThongTinNguoiDungTheoTenDangNhap(tendangnhap);
            int mand = int.Parse(ndDTO.MaNguoiDung.ToString());

            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            dt = hdBUS.SanPhamCuaKhachHangDaMua(mand);
            DataColumn c1 = dt1.Columns.Add("STT", Type.GetType("System.Int32"));
            DataColumn c2 = dt1.Columns.Add("TenSanPham", Type.GetType("System.String"));
            DataColumn c3 = dt1.Columns.Add("TenLoaiSanPham", Type.GetType("System.String"));
            DataColumn c4 = dt1.Columns.Add("TenNSX", Type.GetType("System.String"));
            DataColumn c5 = dt1.Columns.Add("GiaBan", Type.GetType("System.Double"));
            DataColumn c6 = dt1.Columns.Add("SoLuong", Type.GetType("System.Int32"));
            DataColumn c7 = dt1.Columns.Add("ThoiGianMua", Type.GetType("System.DateTime"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int masp = Convert.ToInt32(dt.Rows[i]["MaSanPham"].ToString());
                SanPhamDTO spDTO = new SanPhamDTO();
                spDTO = SanPhamBUS.LaySanPhamTheoMa(masp);
                LoaiSanPhamDTO lspDTO = new LoaiSanPhamDTO();
                lspDTO = lspBUS.LayLoaiSanPhamTheoMa(spDTO.MaLoaiSanPham);
                NhaSanXuatDTO nsxDTO = new NhaSanXuatDTO();
                nsxDTO = nsxBUS.LayNhaSanXuatTheoMa(spDTO.MaNhaSanXuat);
                DataRow r = dt1.NewRow();
                r[0] = (i + 1).ToString();
                r[1] = spDTO.TenSanPham.ToString();
                r[2] = lspDTO.TenLoaiSanPham.ToString();
                r[3] = nsxDTO.TenNhaSanXuat.ToString();
                int slmua = Convert.ToInt32(dt.Rows[i]["SoLuongBan"].ToString());
                r[4] = spDTO.GiaBan.ToString();
                r[5] = slmua.ToString();
                r[6] = dt.Rows[i]["ThoiGianMua"];
                dt1.Rows.Add(r);
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int stt = Convert.ToInt32(gvSanPham.DataKeys[e.Row.RowIndex].Value);
                e.Row.Cells[0].Text = stt.ToString();
                e.Row.Cells[1].Text = dt1.Rows[stt - 1]["TenSanPham"].ToString();
                e.Row.Cells[2].Text = dt1.Rows[stt - 1]["TenLoaiSanPham"].ToString();
                e.Row.Cells[3].Text = dt1.Rows[stt - 1]["TenNSX"].ToString();
                e.Row.Cells[4].Text = dt1.Rows[stt - 1]["GiaBan"].ToString() + " VNĐ";
                e.Row.Cells[5].Text = dt1.Rows[stt - 1]["SoLuong"].ToString();
                e.Row.Cells[6].Text = dt1.Rows[stt - 1]["ThoiGianMua"].ToString();
            }
        }

        protected void gvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSanPham.PageIndex = e.NewPageIndex;
            LoadDS();
        }
    }
}
